package com.trainingcloud.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainingcloud.mapper.TaskMapper;
import com.trainingcloud.mapper.TaskWithStudentMapper;
import com.trainingcloud.mapper.UserMapper;
import com.trainingcloud.pojo.Task;
import com.trainingcloud.pojo.TaskWithStudent;
import com.trainingcloud.pojo.User;
import com.trainingcloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 任务管理表 前端控制器
 * </p>
 *
 * @author 纸鹤
 * @since 2022-10-11
 */
@RestController
public class TaskController {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TaskWithStudentMapper taskWithStudentMapper;
    //根据班级查询任务
    @RequestMapping("/getTasksByTeacherIdAndClassId")
    public ResultUtil getTasksByTeacherIdAndClassId(Task task) {
        ResultUtil ok = ResultUtil.ok();
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        if(task.getRelationTeacherId() != null){
            queryWrapper.eq("relation_teacher_id",task.getRelationTeacherId());
        }
        queryWrapper.eq("relation_class_id",task.getRelationClassId());

        List<Task> tasks = taskMapper.selectList(queryWrapper);
        ok.setData(tasks);
        return ok;
    }
    //添加任务
    @RequestMapping("/addTask")
    public ResultUtil addTask(Task task) {
        //1.将task数据写入数据库
        task.setStartTime(new Date());
        task.setTaskStatus(1);
        taskMapper.insert(task);
        //2.根据task里关联的班级信息，添加taskwithstudent表数据
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", 0);
        queryWrapper.eq("status", task.getRelationClassId());
        List<User> users = userMapper.selectList(queryWrapper);
        for (User user : users) {
            TaskWithStudent student = new TaskWithStudent();
            student.setRelationStudentId(user.getId());
            student.setRelationTaskId(task.getId());
            taskWithStudentMapper.insert(student);
        }
        return ResultUtil.ok("添加成功");
    }

    //获取班级任务列表
    @RequestMapping("/getTaskByClassId")
    public ResultUtil getTaskByClassId(int id, int tid) {
        ResultUtil ok = ResultUtil.ok();
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_teacher_id", tid);
        queryWrapper.eq("relation_class_id", id);
        List<Task> tasks = taskMapper.selectList(queryWrapper);
        ok.setData(tasks);
        return ok;
    }

    //    修改任务列表
    @RequestMapping("/editTask")
    public ResultUtil editTask(Task task) {
        ResultUtil ok = ResultUtil.ok();
        taskMapper.updateById(task);
        return ok;
    }

    //    删除任务列表
    @RequestMapping("/delTask")
    public ResultUtil delTask(int taskId) {
        ResultUtil ok = ResultUtil.ok();
        taskMapper.deleteById(taskId);
        QueryWrapper<TaskWithStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_task_id", taskId);
        taskWithStudentMapper.delete(queryWrapper);
        return ok;
    }


}



